package com.jydd.liaomingframe.main.fragment;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jydd.liaomingframe.R;
import com.jydd.liaomingframe.main.MainActivity;
import com.lm.liaoming.base.custom.CustomBaseFragment;
import com.lm.liaoming.constant.ConstantValues;
import com.lm.liaoming.location.CustomTencentLocationTool;
import com.lm.liaoming.tool.Utils;

import com.tencent.map.geolocation.TencentLocation;

import java.lang.ref.WeakReference;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * created by 韦敏敏
 * on 2019/11/19
 */
public class FragmentModuleOne extends CustomBaseFragment {
    @BindView(R.id.toolbar_title)
    TextView tvToolBarTitle;
    @BindView(R.id.back)
    LinearLayout back;
    private Handler mHandler;
    private CustomTencentLocationTool mLocation;
    /**
     * 需要申请的权限数组
     */
    protected String[] needPermissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.CHANGE_WIFI_STATE,
    };
    public static FragmentModuleOne newInstance(){
        FragmentModuleOne workFragment = new FragmentModuleOne();
        return workFragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        requestPermissions();
        View view = inflater.inflate(R.layout.fragment_module_one, null);
        ButterKnife.bind(this, view);
        return view;
    }
    private void requestPermissions() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        }
        if (!checkPermissions(needPermissions)) {
            EasyPermissions.requestPermissions(
                    this,
                    "为了应用的正常使用，请允许以下权限。",
                    0,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.CHANGE_WIFI_STATE);
            //没有授权，编写申请权限代码
        } else {
            //已经授权，执行操作代码
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(getActivity() instanceof MainActivity){
            MainActivity activity = (MainActivity) getActivity();
            mHandler = new MyHandler(activity);
            mLocation = new CustomTencentLocationTool(getActivity(), mHandler);
            mLocation.startLocation();
        }
        tvToolBarTitle.setText("拆中");
        tvToolBarTitle.setTextSize(22);
        back.setVisibility(View.INVISIBLE);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 移除定位消息
        if(mHandler != null && mHandler.hasMessages(ConstantValues.GPS_LON_LAT_WHAT_VALUE)){
            mHandler.removeMessages(ConstantValues.GPS_LON_LAT_WHAT_VALUE);
        }
        // 停止定位
        if(mLocation != null)
            mLocation.stopLocation();
    }
    public boolean checkPermissions(String[] permissions) {
        return EasyPermissions.hasPermissions(getContext(), permissions);
    }
    // 静态Handler内部类 + 外部类弱引用，防止内存泄漏
    private static class MyHandler extends Handler {
        private final WeakReference<MainActivity> mActivity;
        private MyHandler(MainActivity activity){
            this.mActivity = new WeakReference<MainActivity>(activity);
        }
        @Override
        public void handleMessage(@NonNull Message msg) {
            MainActivity activity = mActivity.get();
            if(activity != null){
                switch (msg.what){
                    case ConstantValues.GPS_LON_LAT_WHAT_VALUE:
                        TencentLocation location = (TencentLocation) msg.obj;
                        if(location != null){
                            // 经度
                            double lon =  location.getLongitude();
                            // 纬度
                            double lat = location.getLatitude();
                            Utils.i("当前经度：" + lon + ", 当前纬度：" + lat);
                        }
                        break;
                    default:
                        break;

                }


            }
            super.handleMessage(msg);
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        if(mLocation != null)
            mLocation.startLocation();
    }
    @Override
    public void onPause() {
        super.onPause();
        if(mLocation != null)
            mLocation.stopLocation();
    }
}
